package com.itdaan.portal.business.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bouncycastle.util.encoders.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.itdaan.portal.business.domain.Page;
import com.itdaan.portal.business.service.BlogService;
import com.itdaan.portal.common.exception.ViewException;

/**
 * 外部用户业务控制类
 * @author www.itdaan.com
 *
 */
@Controller
@RequestMapping(value = "/omember")
@SessionAttributes
public class OutMemberController {

	private Logger logger = LoggerFactory.getLogger(OutMemberController.class);
	
	@Resource(name = "blogService")
	private BlogService blogService;
	
	@RequestMapping(value = {"/{webId:\\d+}/{author}.html"}, method = RequestMethod.GET)
    public
    ModelAndView show(
            HttpServletRequest request,
            HttpServletResponse response,
            @PathVariable String author,
            @PathVariable Integer webId,
            @RequestParam(value = "curPage", required = false) Integer curPage
    ) throws ViewException {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("out_member/index");
    	try{
    		Page page = new Page();
    		Map<String, Object> conditions = new HashMap<String, Object>();
    		author = new String(Base64.decode(author),"utf-8");
    		conditions.put("author", author);
    		conditions.put("authorId", 0);
    		conditions.put("webId", webId);
    		page.setConditions(conditions);
    		if(curPage == null){
    			curPage = 1;
    		}
    		page.setCurrent(curPage);
    		blogService.findBlogPage(page);
    		modelAndView.addObject("page",page);
    		modelAndView.addObject("author", author);
    		modelAndView.addObject("webId", webId);
    	}catch(Exception e){
    		e.printStackTrace();
			logger.error("后台业务逻辑出现异常", e);
    	}
    	return modelAndView;
	}	
	
}
